package com.fc.mall.portal.service.impl;

import com.fc.mall.exception.ServiceException;
import com.fc.mall.mapper.UmsAgentApplyMapper;
import com.fc.mall.mapper.UmsMemberMapper;
import com.fc.mall.model.UmsAgentApply;
import com.fc.mall.model.UmsAgentApplyExample;
import com.fc.mall.model.UmsMember;
import com.fc.mall.portal.dto.UmsAgentApplyParam;
import com.fc.mall.portal.service.RedisService;
import com.fc.mall.portal.service.SmsService;
import com.fc.mall.portal.service.UmsAgentService;
import com.fc.mall.portal.service.UmsMemberService;
import com.fc.mall.util.DateTimeHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.Date;
import java.util.List;

/**
 * Created by Sunny on 2019/7/17.
 */
@Service
public class UmsAgentServiceImpl implements UmsAgentService {

    @Autowired
    private RedisService redisService;

    @Autowired
    private UmsAgentApplyMapper agentApplyMapper;

    @Autowired
    private UmsMemberService memberService;

    @Autowired
    private SmsService smsService;

    @Autowired
    private UmsMemberMapper memberMapper;

    @Override
    public void applyAgent(UmsAgentApplyParam agentApply) {
        UmsMember currentMember = memberService.getCurrentMember();
        UmsAgentApplyExample example = new UmsAgentApplyExample();
        example.createCriteria().andMemberIdEqualTo(currentMember.getId())
                .andStatusEqualTo(1);

        if(agentApplyMapper.countByExample(example) > 0)
        {
            throw new ServiceException("您已申请，等待审核");
        }

        UmsAgentApply umsAgentApply = new UmsAgentApply();
        umsAgentApply.setMemberId(currentMember.getId());
        umsAgentApply.setNickname(currentMember.getNickname());
        umsAgentApply.setPhone(currentMember.getPhone());
        umsAgentApply.setApplyDate(DateTimeHelper.getEpochTimeInSecond(System.currentTimeMillis()));
        umsAgentApply.setStatus(1);
        agentApplyMapper.insertSelective(umsAgentApply);

        if(!StringUtils.isEmpty(currentMember.getNickname()))
        {
            try {
               String nickName =  URLDecoder.decode(currentMember.getNickname(), "utf-8");
                smsService.approvalNotice(currentMember.getId(), nickName, "申请代理", "等待审核", DateTimeHelper.getStringFromDate(new Date(), DateTimeHelper.PAYLOAD_DATEONLY));
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
        }

    }

    @Override
    public UmsAgentApply getApplyAgent() {
        UmsMember currentMember = memberService.getCurrentMember();

        UmsAgentApplyExample example = new UmsAgentApplyExample();
        example.createCriteria().andMemberIdEqualTo(currentMember.getId())
        .andStatusEqualTo(1);
       List<UmsAgentApply> agentApplyList = agentApplyMapper.selectByExample(example);
       if(null == agentApplyList || agentApplyList.isEmpty())
       {
           return null;
       }
        return agentApplyList.get(0);
    }
}
